package com.turing.usdtpay.mvp.fragment;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseFragment;
import com.turing.usdtpay.entity.CommissionManagerData;
import com.turing.usdtpay.entity.MoneyData;
import com.turing.usdtpay.mvp.contract.MoneyContract;
import com.turing.usdtpay.mvp.presenter.MoneyPresent;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;


/**
 * @作者：JAY
 * @创建时间：2019/10/15 12:06
 * @描述：
 **/
public class MoneyFragment extends BaseFragment implements MoneyContract.View {

    private MoneyContract.Present mPresent;
    private TextView mManage,mProperty,mDayProfit,mTotleProfit,mRest,mCny,mUsdt;
    private double mRate;
    public static MoneyFragment newInstance(Bundle bundle){
        MoneyFragment fragment = new MoneyFragment();
        if (bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new MoneyPresent(this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_money, null);
        initView(view);
        initTitle();
        initData();
        return view;
    }

    private void initData() {
        mPresent.getData(getActivity());
    }

    private void initTitle() {
        setTitle("钱包");
        showTitleLogo(false);
        showBackBtn(false);
    }

    private void initView(View view) {
        mManage = (TextView) view.findViewById(R.id.tv_manage);
        mProperty = (TextView) view.findViewById(R.id.tv_property);
        mDayProfit = (TextView) view.findViewById(R.id.day_profit);
        mTotleProfit = (TextView) view.findViewById(R.id.total_profit);
        mRest = (TextView) view.findViewById(R.id.tv_rest);
        mCny = (TextView) view.findViewById(R.id.tv_cny);
        mUsdt = (TextView) view.findViewById(R.id.tv_usdt);
        mManage.setOnClickListener(this);
        mProperty.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == mManage.getId()){
            openNewFragment(CommissionManagerFragment.newInstance(null));
        }else if (v.getId() == mProperty.getId()){
            openNewFragment(PropertyFragment.newInstance(null));
        }
    }

    @Override
    public void onSuccess(MoneyData data) {
        mRate = (double) data.getMoney_change_usdt()/100;
        mDayProfit.setText(FormatUtils.NoumberFromat2((double)data.getDay_commission()/100));
        mTotleProfit.setText(FormatUtils.NoumberFromat2((double)data.getCount_commission()/100));
        mRest.setText(FormatUtils.NoumberFromat2((double)data.getMoney()/100));
        mCny.setText("≈CNY "+FormatUtils.NoumberFromat2((double)data.getMoney()/100));
        double usdt = ((double)data.getMoney()/100/mRate);
        mUsdt.setText(FormatUtils.NoumberFromat8(usdt)+"");
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(getActivity(),msg);
    }

    @Override
    public void getData(CommissionManagerData data) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("data",data);
        openNewFragment(CommissionManagerFragment.newInstance(bundle));
    }

    @Override
    public void setPresenter(MoneyContract.Present presenter) {
        mPresent = presenter;
    }
}
